package com.example.cdctestsystem.controller;

import com.example.cdctestsystem.pojo.UserInfo;
import com.example.cdctestsystem.service.UserInfoService;
import com.example.cdctestsystem.util.CookieUtil;
import com.example.cdctestsystem.util.MD5Util;
import com.example.cdctestsystem.util.MailUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@Controller
@RequestMapping("/services")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;

    @RequestMapping("index")
    public void login() {
    }
    @RequestMapping("user_info")
    public void user_info() {
    }
    @RequestMapping("update_user_info")
    public void update_user_info() {
    }
    @RequestMapping("update_password")
    public void update_password() {
    }
    @RequestMapping("my_collection")
    public void my_collection() {
    }
    @RequestMapping("examination_record")
    public void examination_record() {
    }
    @RequestMapping("ForgetPassword")
    public void forgetPassword(){

    }
    @RequestMapping("ForgetPassword2")
    public void forgetPassword2(){

    }
    @RequestMapping("goForgetPassword")
    public ModelAndView ForgetPassword(String email, HttpSession session, String checkbox, HttpServletResponse response) {
        UserInfo user = new UserInfo();
        user.setEmail(email);
        UserInfo loginUser = userInfoService.login(user);
        ModelAndView modelAndView = new ModelAndView();
        if (loginUser != null) {
            modelAndView.setViewName("/services/login");
            return modelAndView;
        } else {
            String err = "0";
            modelAndView.addObject("err", err);
            modelAndView.setViewName("/services/ForgetPassword");
            return modelAndView;
        }
    }
    //用户忘记密码,验证邮箱账号和验证码,正确到输入密码的页面
    @RequestMapping("isForgetCode")
    public ModelAndView isForgetCode(String emailCode,String email,HttpSession session) throws IOException {
        ModelAndView modelAndView=new ModelAndView();
        String code = (String)session.getAttribute("code");
        if(emailCode.equalsIgnoreCase(code)){
            modelAndView.addObject("email",email);
            modelAndView.setViewName("/services/ForgetPassword2");
            return modelAndView;
        }else {
            modelAndView.setViewName("/services/ForgetPassword");
            return modelAndView;
        }
    }
//--------------------------------------------------------------------------------
    @RequestMapping("isForgetChange")
    public ModelAndView isForgetChange(String newpassword, String Confirmpassword,String email,HttpSession session) throws IOException {
        ModelAndView modelAndView=new ModelAndView();
        if (newpassword.equals(Confirmpassword))
{
    UserInfo user = new UserInfo();
    user.setEmail(email);
    user.setPassword(newpassword);
    userInfoService.update(user);
    modelAndView.setViewName("/services/index");
    return modelAndView;
}
else {
    modelAndView.setViewName("/services/ForgetPassword2");
    return modelAndView;
}
    }

        @RequestMapping("regist")
    public String regist(UserInfo userInfo)
        {
        userInfoService.insert(userInfo);
        return "redirect:/services/index";
    }

    @RequestMapping("isEmail")
    @ResponseBody
    public String isEmail(UserInfo userInfo) {
        UserInfo temp = userInfoService.isEmail(userInfo);
        if (temp == null) {
            return "0";
        } else {
            return "1";
        }
    }
    //用户忘记密码发送验证码
    @RequestMapping("sendUpdateEmail")
    public void sendUpdatePassEmail(UserInfo userInfo, HttpSession session,HttpServletResponse response) throws IOException {
        UserInfo use = userInfoService.isEmail(userInfo);
        if (use != null) {
            String code = MailUtil.getMailUtil().createFoursRandom();
            try {
                MailUtil.getMailUtil().sendMail("验证码", MailUtil.getMailUtil().getMailContent(code), userInfo.getEmail());
                session.setAttribute("code",code);
            }catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            response.getWriter().print("0");
        }
    }
    @RequestMapping("sendEmail")
    public void sendEmail(UserInfo userInfo, HttpSession session,HttpServletResponse response) throws IOException {
        UserInfo temp = userInfoService.isEmail(userInfo);
        if (temp == null) {
            String code = MailUtil.getMailUtil().createFoursRandom();
            try {
                MailUtil.getMailUtil().sendMail("验证码", MailUtil.getMailUtil().getMailContent(code), userInfo.getEmail());
                session.setAttribute("code",code);
            }catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            response.getWriter().print("0");
        }
    }
    @RequestMapping("isEmailCode")
    public void isEmailCode(String emailCode,HttpSession session,HttpServletResponse response) throws IOException {
        String code = (String)session.getAttribute("code");
        if(emailCode.equalsIgnoreCase(code)){
            response.getWriter().print("0");
        }else {
            response.getWriter().print("1");
        }
    }


}
